package datamining.code.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import datamining.code.connection.OracleConnection;
import datamining.code.model.Publicacao;

public class PublicacaoDAO {
	Connection connection = OracleConnection.getConnection();
	
	public ArrayList<Object> retrieve() throws SQLException {
		ArrayList<Object> result = new ArrayList<Object>();
		String sql = "SELECT codigo, autor, editor, titulo, tipo, numchamada FROM publica order by codigo";
		PreparedStatement stmt = connection.prepareStatement(sql);
		ResultSet rs = stmt.executeQuery();
		while (rs.next()) {
			Publicacao publicacao = new Publicacao();
			publicacao.setCodigo(rs.getInt("codigo"));
			publicacao.setAutor(rs.getInt("autor"));
			publicacao.setEditor(rs.getInt("editor"));
			publicacao.setTitulo(rs.getString("titulo"));
			publicacao.setTipo(rs.getString("tipo"));
			publicacao.setNumChamada(rs.getString("numchamada"));
//			System.out.println("Codigo: "+publicacao.getCodigo()+" Numero chamada: "+publicacao.getNumchamada());
			result.add(publicacao);
		}
		System.out.println(result.size());
		if (result.size()>0) {
			return result;
		}
			return null;
	}	
	
	public void insert(Publicacao p) throws SQLException {
		String sql = "INSERT INTO dados(idpublicacao, idautor, ideditor, titulo, tipo, assunto) VALUES(?,?,?,?,?,?)";
		PreparedStatement stmt = connection.prepareStatement(sql);
		stmt.setInt(1, p.getCodigo());
		stmt.setInt(2, p.getAutor());
		stmt.setInt(3, p.getEditor());
		stmt.setString(4, p.getTitulo());
		stmt.setString(5, p.getTipo());
		stmt.setString(6, p.getNumChamada());
		stmt.execute();
		stmt.close();
	}

}
